package info.qingshui.browser.hk.setting;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import socks.Socks5Proxy;
import socks.UserPasswordAuthentication;

import com.wjc.httpserver.HttpServer;

import info.qingshui.browser.hk.StringUtil;
import info.qingshui.browser.hk.proxy.ProxySettings2;

import android.net.Uri;
import android.util.Log;
import android.webkit.WebView;



public class VHostInfo implements  java.io.Serializable{
	private static final long serialVersionUID = 5436702301082736949L;
	
	private String host = "";
	private String proxyServers = "";//host:port:port;host:port:port
	private String ua = null;//T/F/ua:重写UA的值，T透传；F：空；其他：重写
	
	private static final String username = "qingshui";
	private static final String password = "qingshui";
	

	private static List<Socks5Proxy> socks5Proxys ;
	
	private synchronized Socks5Proxy getDefaultSocks5Proxy() {
		if( true ) {
			return newSocks5Proxy("112.121.189.69",53);
		}
		
		if (  socks5Proxys == null ) {
			socks5Proxys = new ArrayList< Socks5Proxy> ();
			for( int i=0; i<20; i++ ) {
				socks5Proxys.add( newSocks5Proxy("182.16.20.108", 2000 + i) );
			}
		}
		//随机返回一个
		return socks5Proxys.get( new Random().nextInt( socks5Proxys.size() ) );
		
	}
	
	public String toString() {
		return "VHostInfo{host=" + host
		+ ";host=" + host
		+ ";proxyServers=" + proxyServers
		+ ";ua=" + ua;
	}
	
	
	public boolean loadWebview(WebView webView,Setting setting, String url) {
		if( ua.equals("F")) {
			webView.getSettings().setUserAgentString("" + Setting.UA_TAIL);
		}
		ProxySettings2.setProxy(webView.getContext(), "0.0.0.0", 18080);
		webView.loadUrl( url );
		return true;
	}
	
	public synchronized Socks5Proxy getSocks5Proxy() {
		return this.getDefaultSocks5Proxy();
	}
	
	private  Socks5Proxy newSocks5Proxy( ) {
		try {
			//随机得到代理IP和端口
			if ( this.proxyServers == null ) {
				return null;
			}
			
			String[] servers = this.proxyServers.split(";");
			if( servers.length <1 ) {
				return null;
			}
			int rIndex = new Random().nextInt( servers.length );
			String[] hostPorts = servers[rIndex].split(":");
			if( hostPorts.length <2 ) {
				return null;
			}
			String proxy = hostPorts[0];
			int port = Integer.parseInt( hostPorts[  new Random().nextInt( hostPorts.length -1 ) + 1] );
			Socks5Proxy proxySet = new Socks5Proxy(null, proxy, port);

			// 认证设置,如果username为空则不添加
			if (username != null) {
				UserPasswordAuthentication k = new UserPasswordAuthentication(
						username, password);
				proxySet.setAuthenticationMethod(
						UserPasswordAuthentication.METHOD_ID, k);
			}
			// store(proxy,port,username,password,context,localPort);
			((Socks5Proxy) proxySet).resolveAddrLocally(false);
			return proxySet;
		} catch (Exception e) {
			Log.e(HttpServer.class.getSimpleName(), "HttpServer()", e);
		}

		return null;
	}


	public String getHost() {
		return host;
	}


	public void setHost(String host) {
		this.host = host;
	}


	public String getProxyServers() {
		return proxyServers;
	}


	public void setProxyServers(String proxyServers) {
		this.proxyServers = proxyServers;
	}


	public String getUa() {
		return ua;
	}


	public void setUa(String ua) {
		this.ua = ua;
	}
	
	
	private Socks5Proxy newSocks5Proxy(String proxy, int port) {
		try {

			Socks5Proxy proxySet = new Socks5Proxy(null, proxy, port);
			// 认证设置,如果username为空则不添加
			if (username != null) {
				UserPasswordAuthentication k = new UserPasswordAuthentication(
						username, password);
				proxySet.setAuthenticationMethod(
						UserPasswordAuthentication.METHOD_ID, k);
			}
			// store(proxy,port,username,password,context,localPort);
			((Socks5Proxy) proxySet).resolveAddrLocally(false);
			return proxySet;
		} catch (UnknownHostException e) {
			Log.e(HttpServer.class.getSimpleName(), "HttpServer()", e);
		}

		return null;
	}
	
}
